#include <cmath>
#include "nr_c/nr.h"
#include "function.h"
#include "nr_c/downhillsimplex.h"
#include "dosh_simplex.h"

static Function *simplexFunction;


static bool isSimplexAllocated = false;

static DownhillSimplex *l_DS;


// used only in this file to do CGM
static double zzfunc(double *x)  
{
  return (simplexFunction->Value(x));
  //return (simplexFunction->NormValue(x));
}


void wrapperSimplex(double *solLocalMinimize, int dim, double ftol, const double StartSize, double &funcVal, Function* func)
{
  simplexFunction = func;

  if (!isSimplexAllocated)
  {
    isSimplexAllocated = true;
    l_DS = new DownhillSimplex(dim);
  }


  l_DS->Eval(solLocalMinimize, funcVal, dim, ftol, StartSize, zzfunc);
}


// used only in this file to do Dosh simplex
extern "C" double __stdcall SA_FCN(int& N, double *x) 
//static double wrapFunctionValueMethod(int& N, double *x)  
{
  return (simplexFunction->Value(x-1));
}

void wrapperDoshSimplex(double *X, const double& steplength, int& N, double& TheChiSqd, Function* func)
{
  simplexFunction = func;

  //SASIMOPT(X+1, steplength, N, TheChiSqd, SA_FCN);


}